package com.dehui.traders.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.bean.OptionalStockBean;
import com.dehui.traders.databinding.FragmentBuySellBinding;
import com.dehui.traders.event.Events;
import com.dehui.traders.event.RxBus;
import com.dehui.traders.ui.viewmodle.BuySellViewModel;
import com.dehui.traders.utils.SimulatedDataManager;
import com.dehui.traders.widgets.BaseViewHolder;
import com.dehui.traders.widgets.CommonAdapter;
import com.dehui.traders.widgets.MyHScrollView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.Utils;
import rx.Subscription;

/**
 * Created by hqq on 18.4.4.
 */

public class BuySellFragment extends BaseFragment<FragmentBuySellBinding,BuySellViewModel>{

    protected List<MyHScrollView> mHScrollViews = new ArrayList<>();
    public HorizontalScrollView mTouchView;
    private Subscription rxSbscription;
    private BuySellViewModel viewModel;
    private List<OptionalStockBean> mList = new ArrayList<>();
    private CommonAdapter<OptionalStockBean> mStockAdapter;

    @Override
    public int initContentView() {
        return R.layout.fragment_buy_sell;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public BuySellViewModel initViewModel() {
        viewModel = new BuySellViewModel(getContext());
        return viewModel;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
        initKLine();
        rxSbscription = RxBus.getInstance().toObserverable(Events.class)
                .filter(events -> events.what != Events.EventEnum.ADD_STOCK)
                .subscribe(events -> {
                    mList = SimulatedDataManager.addOneData(mList);
                    mStockAdapter.notifyDataSetChanged();
                });

    }

    private void initKLine() {
        viewModel.initChart(binding.klineChartK,binding.klineChartVolume);
    }

    @Override
    public void onDestroy() {
        if (!rxSbscription.isUnsubscribed()){
            rxSbscription.unsubscribe();
        }
        super.onDestroy();
    }

    private void initViews() {

        mList = SimulatedDataManager.getOptionalStockList();

        int widthPixels = Utils.getScreen(getContext()).widthPixels;
        MyHScrollView scrollview = binding.getRoot().findViewById(R.id.horizontal_scrollview);
//        scrollview.setPadding(widthPixels/2,0,0,0);
        scrollview.setFragemnt(this);
        LinearLayout llScroll = binding.getRoot().findViewById(R.id.ll_scroll);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) llScroll.getLayoutParams();
        lp.width = widthPixels / 4 * 20;
        llScroll.setLayoutParams(lp);
        mHScrollViews.add(scrollview);

        mStockAdapter = new CommonAdapter<OptionalStockBean>(getContext(), mList, R.layout.item_buy_sell) {
            @Override
            public void convert(BaseViewHolder helper, OptionalStockBean item, int position) {
                LinearLayout llScroll = helper.getView(R.id.ll_scroll);
                LinearLayout llContainer = helper.getView(R.id.ll_container);
                llContainer.setBackgroundColor(Color.BLACK);
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) llScroll.getLayoutParams();
                lp.width = widthPixels / 4 * 20;
                llScroll.setLayoutParams(lp);

                MyHScrollView scrollview = helper.getView(R.id.horizontal_scrollview);
                scrollview.setPadding(widthPixels/2,0,0,0);
                scrollview.setFragemnt(BuySellFragment.this);
                addScrollViews(scrollview);

                LinearLayout llBroader = helper.getView(R.id.ll_broader);
                TextView tvO = helper.getView(R.id.tv_0);
                TextView tv1 = helper.getView(R.id.tv_1);
                TextView tv2 = helper.getView(R.id.tv_2);
                TextView tv3 = helper.getView(R.id.tv_3);
                TextView tv4 = helper.getView(R.id.tv_4);
                TextView tv5 = helper.getView(R.id.tv_5);
                TextView tv6 = helper.getView(R.id.tv_6);
                TextView tv7 = helper.getView(R.id.tv_7);
                TextView tv8 = helper.getView(R.id.tv_8);
                TextView tv9 = helper.getView(R.id.tv_9);
                TextView tv10 = helper.getView(R.id.tv_10);
                TextView tv11 = helper.getView(R.id.tv_11);
                TextView tv12 = helper.getView(R.id.tv_12);
                TextView tv13 = helper.getView(R.id.tv_13);
                TextView tv14 = helper.getView(R.id.tv_14);
                TextView tv15 = helper.getView(R.id.tv_15);
                TextView tv16 = helper.getView(R.id.tv_16);
                TextView tv17 = helper.getView(R.id.tv_17);

                llBroader.setOnClickListener(v->{

                });
                llScroll.setOnClickListener(v->{

                });

                String title = item.name + "\n" + item.code;
                SpannableString styledText = new SpannableString(title);
                styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style_title_0), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style_title_1), 4, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvO.setText(styledText, TextView.BufferType.SPANNABLE);

                tv1.setText(item.datas[0]);
                tv2.setText(item.datas[1]);
                tv3.setText(item.datas[2]);
                tv4.setText(item.datas[3]);
                tv5.setText(item.datas[4]);
                tv6.setText(item.datas[5]);
                tv7.setText(item.datas[6]);
                tv8.setText(item.datas[7]);
                tv9.setText(item.datas[8]);
                tv10.setText(item.datas[9]);
                tv11.setText(item.datas[10]);
                tv12.setText(item.datas[11]);
                tv13.setText(item.datas[12]);
                tv14.setText(item.datas[13]);
                tv15.setText(item.datas[14]);
                tv16.setText(item.datas[15]);
                tv17.setText(item.datas[16]);
                tv1.setTextColor(Color.RED);
                tv2.setTextColor(Color.RED);
                tv3.setTextColor(Color.RED);
                tv4.setTextColor(Color.YELLOW);
                tv5.setTextColor(Color.YELLOW);
                tv6.setTextColor(Color.BLUE);
                tv7.setTextColor(Color.YELLOW);
                tv8.setTextColor(Color.RED);
                tv9.setTextColor(Color.RED);
                tv10.setTextColor(Color.RED);
                tv11.setTextColor(Color.GREEN);
                tv12.setTextColor(Color.YELLOW);

            }
        };
        binding.lvOptionalStock.setAdapter(mStockAdapter);

    }

    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        for(MyHScrollView scrollView : mHScrollViews) {
            if(mTouchView != scrollView)
                scrollView.smoothScrollTo(l, t);
        }
    }

    private void addScrollViews(MyHScrollView scrollview) {
        if (!mHScrollViews.isEmpty()) {
            int size = mHScrollViews.size();
            MyHScrollView scrollView = mHScrollViews.get(size - 1);
            final int scrollX = scrollView.getScrollX();
            //第一次满屏后，向下滑动，有一条数据在开始时未加入
            if (scrollX != 0) {
                binding.lvOptionalStock.post(new Runnable() {
                    @Override
                    public void run() {
                        //当listView刷新完成之后，把该条移动到最终位置
                        scrollView.scrollTo(scrollX, 0);
                    }
                });
            }
        }
        mHScrollViews.add(scrollview);
    }


}
